package com.onemile.controls;

import java.util.ArrayList;
import java.util.HashMap;
import com.onemile.app.R;
import com.onemile.app.UICart;
import com.onemile.entity.Session;
import com.onemile.entity.UrlParams;
import com.onemile.entity.model.EAttributeDetails;
import com.onemile.entity.model.ECommodity;
import com.onemile.entity.model.ECommodityAccessories;
import com.onemile.net.NetImageLoaderM;
import com.onemile.utils.UtiDialogsFactory;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdpLV_Cart extends BaseAdapter
{

   private ArrayList<ECommodity> mList;
   private LayoutInflater mInflater;
   private Context mContext;

   public AdpLV_Cart(ArrayList<ECommodity> list, Context context)
   {
	  mList = list;
	  mContext = context;
	  mInflater = LayoutInflater.from(context);
   }
   
   public void addItem(ArrayList<ECommodity> _list)
   {
	  if (mList != null)
	  {
		 for (ECommodity eGood : _list)
		 {
			mList.add(eGood);
		 }
	  }
   }

   @Override
   public int getCount()
   {
	  return mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int arg0)
   {
	  return 0;
   }

   @SuppressLint("ParserError")
   @Override
   public View getView(final int position, View currentView, ViewGroup arg2)
   {
	  try
	  {
		 ViewHolder holder;
		 if (currentView == null)
		 {
			currentView = mInflater.inflate(R.layout.cart_item, null);

			holder = new ViewHolder();
			holder.ShopName = (TextView) currentView.findViewById(R.id.txtCommodityName_cart_item);
			holder.Count = (TextView) currentView.findViewById(R.id.txtCount_cart_item);
			holder.ImgDelete = (ImageView) currentView.findViewById(R.id.imgDelete_cart_item);
			holder.Remark = (TextView) currentView.findViewById(R.id.txtRemark_cart_item);

			currentView.setTag(holder);
		 }
		 else
		 {
			holder = (ViewHolder) currentView.getTag();
		 }
		 
	     holder.ShopName.setText(mList.get(position).getCommodityName());
	     holder.Count.setText(String.valueOf(mList.get(position).getCount()));
	     
	     StringBuffer sb=new StringBuffer();
	     if(mList.get(position).getSpecification()!=null)
	     {
	       sb.append(mList.get(position).getSpecification().getSpecificationName());
	       sb.append(",");
	     }
	     if(mList.get(position).getAttributeDetailsList()!=null)
	     {
	    	int size=mList.get(position).getAttributeDetailsList().size();
	    	for (int i=0;i<size;i++)
			{
			   sb.append(mList.get(position).getAttributeDetailsList().get(i).getAttributeDetailsName());
			   sb.append(",");
			}
	     }
	     if(mList.get(position).getCommodityAccessoriesList()!=null)
	     {
	    	int size=mList.get(position).getCommodityAccessoriesList().size();
	    	for (int i=0;i<size;i++)
			{
			   sb.append(mList.get(position).getCommodityAccessoriesList().get(i).getCommodityAccessoriesName());
			   sb.append(",");
			}
	     }
	     holder.Remark.setText(sb.substring(0, sb.length()-1)+"】");
	     holder.ImgDelete.setOnClickListener(new View.OnClickListener()
		 {
		    @Override
		    public void onClick(View v)
		    {
		 	   Session.getInstance().RemoveCommoditys(position);
		 	  ((UICart)mContext).SetDisplay();
		 	   notifyDataSetChanged();
		    }
		 });
	     return currentView;
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "AdpLV_ShopList_getView(position, currentView, arg2) ", e);
		 }
	  }
	  return new View(mContext);
   }

   static class ViewHolder
   {
	  TextView ShopName;
	  TextView Count;
	  TextView Remark;
	  ImageView ImgDelete;
   }

}
